package codetop.huawei.T735;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author: 18362
 * @Create: 2022-09-13 0:27:13 星期二
 */
class Solution {
    public int[] asteroidCollision(int[] asteroids) {
        Deque<Integer> dq = new LinkedList<>();
        for (int cur : asteroids) {
            boolean bothBump = false;
            while (!dq.isEmpty()) {
                int last = dq.peek();
                if (last * cur > 0 || (last < 0 && cur > 0)) {
                    dq.push(cur);
                    break;
                }
                if (last + cur == 0) {
                    dq.pop();
                    bothBump = true;
                    break;
                }
                if (Math.abs(last) > Math.abs(cur))
                    break;
                dq.pop();
            }
            if (dq.isEmpty() && !bothBump)
                dq.push(cur);
        }
        int[] res = new int[dq.size()];
        for (int i = res.length-1; i>=0; --i)
            res[i] = dq.pop();
        return res;
    }
}